package com.i2bt.app;

import com.i2bt.common.UserConfig;

import com.i2bt.R;
import com.i2bt.app.login.LoginActivity;
import com.i2bt.app.login.RegistActivty;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.WindowCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast;

/**
 * 
 * Title : 基础活动抽象类
 * 
 * Description : 定义了要实现初始化视图、逻辑的抽象方法，定义了一些通用的方法
 * 
 * CreatedTime : 2014-8-6
 * 
 * @author wuliao & kakaxi & yi
 * 
 * @version 1.1
 * 
 */

public abstract class BaseActivity extends ActionBarActivity implements
		OnClickListener {

	protected static Context context; // 上下文

	protected ActionBar actionBarSupport;

	protected android.app.ActionBar actionBar;

	public Menu menu;

	protected SharedPreferences settings;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);

		context = getBaseContext();

		if (Build.VERSION.SDK_INT >= 11) {
			actionBar = ((Activity) this).getActionBar();
			if (actionBar != null) {
				actionBar.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.actionbar_back));
				actionBar.setIcon(R.drawable.actionbar_logo);
			}
		} else {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			actionBarSupport = getSupportActionBar();
			if (actionBarSupport != null) {
				actionBarSupport.setBackgroundDrawable(getResources()
						.getDrawable(R.drawable.actionbar_back));
				actionBarSupport.setIcon(R.drawable.actionbar_logo);
			}
		}

	}

	/**
	 * 初始化视图
	 */
	abstract protected void initView();

	/**
	 * 调用业务逻辑处理
	 */
	abstract protected void logic();

	/**
	 * 进入指定活动并携带数据包
	 * 
	 * @param cls
	 * @param bundle
	 *            数据包
	 */
	public void goActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent(getBaseContext(), cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
	}

	/**
	 * 进入指定活动类
	 * 
	 * @param cls
	 *            将打开的活动类
	 */
	public void goActivity(Class<?> cls) {
		this.goActivity(cls, null);
	}

	/**
	 * 进入指定活动类并完成现在的活动并携带数据包
	 * 
	 * @param cls
	 * @param bundle
	 *            数据包
	 */
	public void goActivityAndFinish(Class<?> cls, Bundle bundle) {
		this.goActivity(cls, bundle);
		finish();
	}

	/**
	 * 进入指定活动类并完成现在的活动
	 * 
	 * @param cls
	 */
	public void goActivityAndFinish(Class<?> cls) {
		this.goActivity(cls, null);
		finish();
	}
	
	/**
	 * 携带参数进入指定活动类并要求返回结果
	 * 
	 * @param cls 跳转的活动类
	 * @param bundle 参数
	 * @param requestCode 请求代号
	 */
	public void goActivityAndResult(Class<?> cls, Bundle bundle, int requestCode) {
		Intent intent = new Intent(this, cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivityForResult(intent, requestCode);
	}
	
	/**
	 * 进入指定活动类并要求返回结果
	 * @param cls 跳转的活动类
	 * @param requestCode 请求代号
	 */
	public void goActivityAndResult(Class<?> cls, int requestCode) {
		this.goActivityAndResult(cls, null, requestCode);
	}

	// toast处理器
	private static Handler toastHandler = new Handler() {

		// 处理信息
		public void handleMessage(android.os.Message msg) {
			// 发送toast
			Toast.makeText(context, msg.obj.toString(), Toast.LENGTH_SHORT)
					.show();
		};
	};

	/**
	 * 调用toastHandler弹出toast
	 * 
	 * @param text
	 *            toast的信息
	 */
	public void toastHandler(String text) {
		toastHandler.obtainMessage(0, text).sendToTarget();
	}

	@SuppressLint("NewApi")
	public void setActionBarTitle(String titleName) {
		if (Build.VERSION.SDK_INT < 11) {
			actionBarSupport.setTitle(titleName);
		} else {
			actionBar.setTitle(titleName);
		}
	}

	@SuppressLint("NewApi")
	public void setShowBackBtn(boolean isShow) {
		if (Build.VERSION.SDK_INT < 11) {
			actionBarSupport.setDisplayHomeAsUpEnabled(isShow);
		} else {
			actionBar.setDisplayHomeAsUpEnabled(isShow);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.menu = menu;
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * 点击actionbar的回退按钮调用的方法
	 */
	public abstract void backHomeOperate();

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			backHomeOperate();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void setActionMenuGroupVisible(boolean isShow) {
		menu.setGroupVisible(R.id.group_main_action, isShow);
		menu.setGroupVisible(R.id.group_files_checked, isShow);
		menu.setGroupVisible(R.id.group_files_action, isShow);
//		menu.setGroupVisible(R.id.group_friend_action, isShow);
	}



	@SuppressLint("NewApi")
	public void setActionBarBack(String Title) {
		if (Build.VERSION.SDK_INT < 11) {
			actionBarSupport.setDisplayShowHomeEnabled(false);
			actionBarSupport.setDisplayHomeAsUpEnabled(true);
		} else {
			actionBar.setDisplayShowHomeEnabled(false);
			actionBar.setDisplayHomeAsUpEnabled(true);
		}
		setActionBarTitle(Title);
	}
	
	@SuppressLint("NewApi")
	public void refreshActionbar() {
		if (Build.VERSION.SDK_INT < 11) {
			actionBarSupport.show();
		} else {
			actionBar.show();
		}
	}

	/**
	 * 检查当前的登陆状态，没有登录时进行后台登陆,保证token的存在
	 */
	public int CheckAndLogin() {
		String userId = this.readXML("ACCOUNT", "USERID");
		String password = this.readXML("ACCOUNT", "PASSWORD");
		UserConfig.userId = userId;
		UserConfig.password = password;
		System.out.println("用户名"+UserConfig.userId);
		System.out.println("密码："+UserConfig.password);
		if (this.getClass() == LoginActivity.class
				|| this.getClass() == RegistActivty.class) {
			return 0;
		}
		if (UserConfig.token.length() > 6) {
			return 0;
		} else {
			return 1;
		}
	}

	/**
	 * 写入xml文件
	 * 
	 * @param name
	 *            xml文件名
	 * @param key
	 *            键
	 * @param value
	 *            键值
	 */
	public void writeXML(String name, String key, String value) {
		settings = getSharedPreferences(name, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 将boolean写入xml中
	 * 
	 * @param name
	 *            xml文件名
	 * @param key
	 *            键名
	 * @param value
	 *            键值
	 */
	public void writeXMLBoolean(String name, String key, Boolean value) {
		settings = getSharedPreferences(name, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 获取xml键值
	 * 
	 * @param name
	 *            xml文件名
	 * @param key
	 *            键名
	 * @return 如果返回空证明键值不存在
	 */
	public String readXML(String name, String key) {
		settings = getSharedPreferences(name, Context.MODE_PRIVATE);
		if (settings.contains(key)) {
			return settings.getString(key, null);
		} else {
			return null;
		}
	}

	/**
	 * 读取xml中的boolean
	 * 
	 * @param name
	 *            xml文件名
	 * @param key
	 *            键名
	 * @return 返回读取的布尔值，不能读取时返回null
	 */
	public Object readXMLBoolean(String name, String key) {
		settings = getSharedPreferences(name, Context.MODE_PRIVATE);
		if (settings.contains(key)) {
			return settings.getBoolean(key, false);
		} else {
			return null;
		}
	}

	/****
	 * 删除xml里面的键值
	 * 
	 * @param name
	 *            xml的文件名
	 * @param key
	 *            键值
	 */
	public void removeXML(String name, String key) {
		settings = getSharedPreferences(name, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.remove(key);
		editor.commit();
	}

}
